package com.atguigu.szdz.dcsyl.controller;

import com.atguigu.szdz.dcsyl.common.utils.R;
import com.atguigu.szdz.dcsyl.service.ProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.headers.Header;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/project")
public class ProjectController {
    @Autowired
    ProjectService projectService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @PostMapping("/getProjectList")
    @Operation(summary = "获取项目列表")
    public R getProjectList(@RequestBody Map<String, Object> map) {
        Integer currentPage = (Integer) map.get("currentPage");
        String keyword = (String) map.get("keyword");
        List<Map<String, Object>> projectList = projectService.getProjectList(currentPage, keyword);
        //查询总条数
        String sql = "select count(*) from dc_project pro "
                + " where (pro.project_name like '%" + keyword + "%')";
        List<Map<String, Object>> total = projectService.queryForObject(sql);
        Object count = total.get(0).get("count");
        Long c = (Long) count;
        return R.ok().put("projectList", projectList).put("total", c);
    }

    @PostMapping("/addProject")
    @Operation(summary = "添加项目")
    public R addProject(@RequestBody Map<String, Object> map, @RequestHeader("userId") String userId) {
        String userName = (String) redisTemplate.opsForHash().get(userId, "userName");
        map.put("createby", userName);
        projectService.addProject(map);
        return R.ok();
    }

    @PostMapping("/updateProject")
    @Operation(summary = "更新项目")
    public R updateProject(@RequestBody Map<String, Object> map, @RequestHeader("userId") String userId) {
        String userName = (String) redisTemplate.opsForHash().get(userId, "userName");
        map.put("updateby", userName);
        projectService.updateProject(map);
        return R.ok();
    }

    @PostMapping("/deleteProject")
    @Operation(summary = "删除项目")
    public R deleteProject(@RequestBody Map<String, Object> map) {
        Integer project_id = (Integer) map.get("project_id");
        projectService.deleteProject(project_id);
        return R.ok();
    }

    //根据项目id查询所有井的信息
    @PostMapping("/getWellListByProjectId")
    @Operation(summary = "根据项目id查询所有井的信息")
    public R getWellListByProjectId(@RequestBody Map<String, Object> map) {
        Integer project_id = (Integer) map.get("project_id");
        List<Map<String, Object>> wellList = projectService.getWellListByProjectId(project_id);
        return R.ok().put("wellInfoList", wellList);
    }


    //根据项目id修改n的信息
    @PostMapping("/updateNByProjectId")
    @Operation(summary = "根据项目id修改n的信息")
    public R updateNByProjectId(@RequestBody Map<String, Object> map) {
        //先根据项目id查询项目信息
        Integer project_id = (Integer) map.get("project_id");
        if (project_id == null) {
            return R.error("项目id不能为空");
        }
        //获取井id
        Integer well_id = map.get("well_id") == null ? 0 : (Integer) map.get("well_id");
        //获取井的nhg和obg数据
        String sql = "select nhg,obg from \"dc_ylDataDc\" well where well_id = " + well_id;
        List<Map<String, Object>> wellInfo = projectService.queryForObject(sql);
        if (wellInfo.size() == 0) {
            return R.error("该井不存在");
        }
        Double nhg = Double.parseDouble(wellInfo.get(0).get("nhg").toString());
        Double obg = Double.parseDouble(wellInfo.get(0).get("obg").toString());
        //获取深度
        Double depth = Double.parseDouble(map.get("depth").toString());
        Double ρp = Double.parseDouble(map.get("ρp").toString());

        //计算压力
        Double pp = ρp * 0.0098 * depth;
        //计算dcn
        Double dcn = Math.pow(Math.E, 0.0004158 * depth - 1.28100587);
        //计算dc
        Double dc = ((Math.log(3.282 / 55.0 / 10)) / Math.log(0.0684 * 15 / 210)) * (1.7 / 1.03) - 0.649;

        //计算n
        Double n = Math.log((obg - pp) / (obg - nhg)) / Math.log(dc / dcn);
        map.put("n", n);
        projectService.updateNByProjectId(map);
        return R.ok();
    }
}
